package com.wwcom.darteagle.util;

import java.util.ArrayList;
import java.util.List;

import com.wwcom.darteagle.R;
import com.wwcom.darteagle.model.Match;
import com.wwcom.darteagle.model.MatchPlayer;
import com.wwcom.darteagle.model.Player;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;

public class Settings {
	private final static String PREFS_NAME = "DartEaglePrefs"; 
	public static String theme;
	public static MatchSettings matchSettings;
	
		
    public static void loadSettings(Context context) {
        // Restore preferences
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
        Settings.theme= prefs.getString("theme", "PDC");   
        //MatchSettings
        MatchSettings.players= TextUtils.split(prefs.getString("matchSettings_players", ""),"#,#");
        MatchSettings.nSets= prefs.getInt("matchSettings_nSets", -1);
        MatchSettings.nLegs= prefs.getInt("matchSettings_nLegs", 5);
        MatchSettings.legTarget= prefs.getInt("matchSettings_legTarget", 501);
        MatchSettings.doubleIn= prefs.getBoolean("matchSettings_doubleIn", false);
        MatchSettings.doubleOut= prefs.getBoolean("matchSettings_doubleOut", true);
        MatchSettings.trebleIn= prefs.getBoolean("matchSettings_trebleIn", false);
        MatchSettings.trebleOut= prefs.getBoolean("matchSettings_trebleOut", false);
        MatchSettings.loserStarts= prefs.getBoolean("matchSettings_loserStarts", false);
        //StatsSettings
        StatsSettings.players= TextUtils.split(prefs.getString("statsSettings_players", ""),"#,#");
        StatsSettings.selectedStat= prefs.getInt("statsSettings_selectedStat", 0);
        
    }
    
    public static void saveSettings(Context context) {
        // We need an Editor object to make preference changes.
        // All objects are from android.context.Context
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("theme", Settings.theme);
        
        //matchSettings
        editor.putString("matchSettings_players", TextUtils.join("#,#",MatchSettings.players));
        editor.putInt("matchSettings_nSets", MatchSettings.nSets);
        editor.putInt("matchSettings_nLegs", MatchSettings.nLegs);
        editor.putInt("matchSettings_legTarget", MatchSettings.legTarget);
        editor.putBoolean("matchSettings_doubleIn", MatchSettings.doubleIn);
        editor.putBoolean("matchSettings_doubleOut", MatchSettings.doubleOut);
        editor.putBoolean("matchSettings_trebleIn", MatchSettings.trebleIn);
        editor.putBoolean("matchSettings_trebleOut", MatchSettings.trebleOut);
        editor.putBoolean("matchSettings_loserStarts", MatchSettings.loserStarts);
        
        //statsSettings
        editor.putString("statsSettings_players", TextUtils.join("#,#",StatsSettings.players));
        editor.putInt("statsSettings_selectedStat", StatsSettings.selectedStat);
        

        // Commit the edits!
        editor.commit();    	
    }
    
	public static int getThemeID(Context context) {
  	    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        String theme= prefs.getString("themePref", "PDC");    	
		
		if (theme.equals("PDC")) {
			return R.style.Theme_SKY;
		} else if (theme.equals("PDC.WC2012")) {
			return R.style.Theme_SKYWC2012;			
		} else {
			return R.style.Theme_BBC;			
		}
	}
    
	public static class MatchSettings {
		public static String[] players;
		public static int nSets;
		public static int nLegs;
		public static int legTarget;
		public static boolean doubleIn; 
		public static boolean doubleOut; 
		public static boolean trebleIn; 
		public static boolean trebleOut; 
		public static boolean loserStarts;
	
		public static void updateSettings(Match m) {
			List<MatchPlayer> mp= m.getMatchPlayers();
			players= new String[mp.size()];
			int i= 0;
			for (MatchPlayer p : mp) {
				players[i]= p.getPlayer().getName();
				i++;
			}
			nSets= m.getNumSets();
			nLegs= m.getNumLegs();
			legTarget= m.getLegTarget();
			doubleIn= m.isDoubleIn();
			doubleOut= m.isDoubleOut();
			trebleIn= m.isTrebleIn();
			trebleOut= m.isTrebleOut();	
			loserStarts= m.getMatchInfo().isLoserStarts();
		}
	}

	public static class StatsSettings {
		public static String[] players;
		public static int selectedStat;
	
		public static void updateSettings(List<Player> playerList, int newSelectedStat) {
			players= new String[playerList.size()];
			int i= 0;
			for (Player p : playerList) {
				players[i]= p.getName();
				i++;
			}
			selectedStat= newSelectedStat;
		}
		
		public static List<Player> getPlayers() {
			List<Player> playerList= new ArrayList<Player>(players.length);
			for (int i= 0; i<players.length; i++) {
				playerList.add(new Player(players[i]));
			}
			return playerList;
		}

		public static int getSelectedStat() {
			return selectedStat;
		}
	
	}

}
